package hjj.servlet;

import hjj.service.UserManager;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.User;

import util.Page;

public class UserManagerServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		int pageNo = 1;
		int pageSize = 10;
		UserManager um=new UserManager();
		Page<User> page=null;
		String userqueryinfo = "";
		String userquerytype = "";
		try {
			if (request.getParameter("pageNo") != null) {
				pageNo = Integer.parseInt(request.getParameter("pageNo"));
			}
			if (request.getParameter("pageSize") != null) {
				pageSize = Integer.parseInt(request.getParameter("pageSize"));

				request.getSession().setAttribute("pageSize", pageSize);
			}
			if (request.getSession().getAttribute("pageSize") != null) {
				pageSize = (Integer) request.getSession().getAttribute(
						"pageSize");
			}
			if (null != request.getParameter("userquerytype")) {
				pageNo = 1;
				request.getSession().setAttribute("userquerytype",
						request.getParameter("userquerytype"));
			}
			if (null != request.getParameter("userqueryinfo")) {
				request.getSession().setAttribute("userqueryinfo",
						request.getParameter("userqueryinfo"));
			}
			if (null != request.getSession().getAttribute("userquerytype")) {
				userquerytype = (String) request.getSession().getAttribute(
						"userquerytype");
			}
			if (null != request.getSession().getAttribute("userqueryinfo")) {
				userqueryinfo = (String) request.getSession().getAttribute(
						"userqueryinfo");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (!"".equals(userqueryinfo)&&!"userall".equals(userquerytype)) {
			if ("username".equals(userquerytype)) {
				page = um.getUserByName(pageNo, pageSize, userqueryinfo);
			}
			if ("userrole".equals(userquerytype)) {
				page = um.getUserByRole(pageNo, pageSize, userqueryinfo);
			}
			if ("userpower".equals(userquerytype)) {
				page = um.getUserByPower(pageNo, pageSize, userqueryinfo);
			}
			if ("userstatus".equals(userquerytype)) {
				page = um.getUserByStatus(pageNo, pageSize, userqueryinfo);
			}

		} else {
			page = um.getAll(pageNo, pageSize);
		}
		request.setAttribute("page", page);
		request.getRequestDispatcher("System/UserManager.jsp").forward(request, response);
	}

}
